(function(jQuery) {
	
	var funNew = null;//调用四级地址时需要条用其他方法

	/*
	 * 四级联动插件
	 */
	$.AddressUtils = {
			
			
			
			/**
			 * 初期化省级地址
			 */
			selectProvince : function(inputId,adressCode) {
				var effectRow = new Object();
				//获取省级下拉框值
				var province = $("#"+inputId[0]+"").val();
        		$.post("address/queryProvince.do?Login_SessionId="+sessionId, effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		$("#"+inputId[0]+"").combobox("clear").combobox('loadData',data);
        	    		var Provinces = data.retdata;
        	    		 var datanew,json;
        	    		 datanew = [];
        	    		 datanew.push({ "text": "", "id":"0"});
        	    		for(var i=0;i<Provinces.length;i++){
        	    			datanew.push({ "text": Provinces[i].firstArea, "id":Provinces[i].firstCode});
        	    		}
        	    		$("#"+inputId[0]+"").combobox("loadData", datanew);
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[0]+"").combobox("setValue",adressCode[0]);
        	    		adressCode[0]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取市
			 */
			selectCity : function(inputId,adressCode) {
				var effectRow = new Object();
				
				//选取省级下拉框值作为查询条件
				var province = $("#"+inputId[0]+"").combobox('getValue');
        		$.post("address/queryCity.do?provinceId="+province+'&Login_SessionId='+sessionId, effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[1]&&inputId[1].length>0){
        	    			$("#"+inputId[1]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var citys = data.retdata;
       	    		 var datanew,json;
    	    		 datanew = [];
        	    		for(var i=0;i<citys.length;i++){
        	    				datanew.push({ "id":citys[i].secondCode, "text": citys[i].secondArea});
        	    		}
        	    		if(null!=inputId[1]&&inputId[1].length>0){
        	    			$("#"+inputId[1]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[1]+"").combobox("setValue",adressCode[1]);
        	    		adressCode[1]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取区
			 */
			selectDistrict : function(inputId,adressCode) {
				var effectRow = new Object();
				var cityId = $("#"+inputId[1]+"").combobox('getValue');
        		$.post("address/queryDistrict.do?cityId="+cityId+'&Login_SessionId='+sessionId, effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var districts = data.retdata;
          	    		 var datanew,json;
        	    		 datanew = [];
        	    		for(var i=0;i<districts.length;i++){
        	    				datanew.push({ "id":districts[i].thirdCode, "text": districts[i].thirdArea});
        	    		}
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[2]+"").combobox("setValue",adressCode[2]);
        	    		adressCode[2]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取街道
			 */
			selectStreet : function(inputId,adressCode) {
				var effectRow = new Object();
				var districtId = $("#"+inputId[2]+"").combobox('getValue');
        		$.post("address/queryStreet.do?districtId="+districtId+'&Login_SessionId='+sessionId, effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var streets = data.retdata;
         	    		 var datanew,json;
        	    		 datanew = [];
        	    		for(var i=0;i<streets.length;i++){
        	    				datanew.push({ "id":streets[i].fouthCode, "text": streets[i].fouthArea});
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[3]+"").combobox("setValue",adressCode[3]);
        	    		adressCode[3]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 页面初始化函数
			 */
			inits : function(inputId,adressCode) {

				if(null!=inputId&&null==adressCode){
					$.AddressUtils.selectProvince(inputId,null);
				}
				if(null!=inputId&&null!=adressCode){
					$.AddressUtils.selectProvince(inputId,adressCode);
            	}
				if(null!=inputId[0]&&inputId[0].length>0){
					$('#'+inputId[0]).combobox({
		                onChange: function (n, o) {
	                		$.AddressUtils.selectCity(inputId,adressCode);
		                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
		                		funNew();
		                	}
		                }
		            });
				}
				if(null!=inputId[1]&&inputId[1].length>0){
					$('#'+inputId[1]).combobox({
		                onChange: function (n, o) {
		                		$.AddressUtils.selectDistrict(inputId,adressCode);
			                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
			                		funNew();
			                	}
		                }
		            });
				}

				if(null!=inputId[2]&&inputId[2].length>0){
					$('#'+inputId[2]).combobox({
		                onChange: function (n, o) {
		                		$.AddressUtils.selectStreet(inputId,adressCode);
			                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
			                		funNew();
			                	}
		                }
		            });
				}

				
			},
			
			
			/**
			 * 页面初始化函数为调用其他方法添加
			 */
			initsNew : function(inputId,adressCode,fun) {
				funNew=fun;
				$.AddressUtils.inits(inputId,adressCode);
			}
			
	};
	
})(jQuery);


